#使用sklearn线性回归实现房价预测模型，根据城市人口,房间数目 来预测房价，
# 数据文件“ex1data2.txt”数据是二个特征，作一个最简单的多元（在此为二元）线性回归。

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from mpl_toolkits.mplot3d import Axes3D
from sklearn.metrics import mean_squared_error,r2_score

plt.rcParams['font.sans-serif'] =['SimHei']
plt.rcParams['axes.unicode_minus'] =False

data = np.loadtxt('./data/ex1data2.txt',delimiter=',')
data_x=data[:, 0:2]
data_y=data[:, 2]

model =LinearRegression()
model.fit(data_x,data_y)

y_predict =model.predict(data_x)


fig =plt.figure()
ax= fig.gca(projection='3d')
ax.scatter(data_x[:,0],data_x[:,1],data_y,color='red')
ax.plot(data_x[:,0],data_x[:,1],y_predict,color='blue')
ax.set_xlabel('城市人口')
ax.set_ylabel('房间数目')
ax.set_ylabel('房价')
plt.title('线性回归--城市人口，房间数目与房价的关系')

plt.show()

print(model.coef_)
print(model.intercept_)
print(mean_squared_error(data_y,y_predict))
print(r2_score(data_y,y_predict))

